package com.transpeed.payservice.pay.alipay;

import com.alipay.api.domain.TradeFundBill;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.demo.trade.model.builder.AlipayTradeQueryRequestBuilder;
import com.alipay.demo.trade.model.result.AlipayF2FQueryResult;
import com.alipay.demo.trade.utils.Utils;
import com.transpeed.payservice.log.Log;

/**
 * TODO 支付订单查询（适用于C扫B和B扫C）
 * @author lizhi
 * @date 2017年4月25日
 * 更新日志
 * 2017年3月2日 lizhi 首次创建
 *
 */
public class QueryAlipayTrade {

	private static String TradeStatus = null;

	// public static void main(String[] args) throws Exception {
	// test_trade_query("tradeprecreate14883400209092770804");
	//
	// }
	public static String test_trade_query(String TradeNumber) {
		// (必填) 商户订单号，通过此商户订单号查询当面付的交易状态
		String outTradeNo = TradeNumber;

		// 创建查询请求builder，设置请求参数
		AlipayTradeQueryRequestBuilder builder = new AlipayTradeQueryRequestBuilder().setOutTradeNo(outTradeNo);
		AlipayF2FQueryResult result = AlipayMain.tradeService.queryTradeResult(builder);

		Log.Write("查询订单号为：" + TradeNumber, "AlipayQRCode");
		Log.Write("result.getTradeStatus()  " + result.getTradeStatus(),"AlipayQRCode");
		Log.Write("result.getResponse().getSubCode()"+ result.getResponse().getSubCode(), "AlipayQRCode");
		Log.Write("result.getResponse().getMsg()"+ result.getResponse().getMsg(), "AlipayQRCode");

		String resultTradeStatus = result.getTradeStatus().toString();
		String TradeSubCode = result.getResponse().getSubCode();
		if (TradeSubCode == null)
			TradeSubCode = "123";
		String TradeMsg = result.getResponse().getMsg();

		// 对收到支付宝回传的信息做判断
		if (resultTradeStatus.equals("SUCCESS") && TradeMsg.equals("Success"))
			TradeStatus = "Alipay&Success&100";
		if (resultTradeStatus.equals("FAILED")
				&& TradeSubCode.equals("ACQ.TRADE_NOT_EXIST"))
			TradeStatus = "Alipay&Failed&109";
		if (resultTradeStatus.equals("FAILED") && TradeMsg.equals("Success"))
			TradeStatus = "Alipay&Failed&108";

		Log.Write(TradeStatus, "AlipayQRCode");

		switch (result.getTradeStatus()) {
		case SUCCESS:
			AlipayMain.log.info("查询返回该订单支付成功: )");
			Log.Write("查询返回该订单支付成功!!! 查询订单号为：" + outTradeNo, "AlipayQRCode");

			AlipayTradeQueryResponse response = result.getResponse();
			AlipayMain.dumpResponse(response);

			Log.Write(response.toString(), "AlipayQRCode");
			AlipayMain.log.info(response.getTradeStatus());
			Log.Write(response.getTradeStatus(), "AlipayQRCode");

			// System.out.println(response.getTradeStatus());
			if (Utils.isListNotEmpty(response.getFundBillList())) {
				for (TradeFundBill bill : response.getFundBillList()) {
					AlipayMain.log.info(bill.getFundChannel() + ":" + bill.getAmount());
					Log.Write(bill.getFundChannel() + ":" + bill.getAmount(),"AlipayQRCode");

				}
			}
			break;

		case FAILED:
			AlipayMain.log.error("查询返回该订单支付失败或被关闭!!!");
			Log.Write("查询返回该订单支付失败或被关闭!!! 查询订单号为：" + outTradeNo,"AlipayQRCode");
			break;

		case UNKNOWN:
			AlipayMain.log.error("系统异常，订单支付状态未知!!!");
			Log.Write("系统异常，订单支付状态未知!!! 查询订单号为：" + outTradeNo,"AlipayQRCode");
			break;

		default:
			AlipayMain.log.error("不支持的交易状态，交易返回异常!!!");
			Log.Write("不支持的交易状态，交易返回异常!!! 查询订单号为：" + outTradeNo,"AlipayQRCode");
			break;
		}

		Log.Write("查询支付宝订单程序执行完毕！查询订单号为：" + outTradeNo + "订单状态为：" + TradeStatus,"AlipayQRCode");
		return TradeStatus;
	}
}
